package com.yuhang.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuhang.common.Result;
import com.yuhang.eduservice.entity.EduTeacher;
import com.yuhang.eduservice.entity.dto.TeacherQuery;
import com.yuhang.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author yyh
 * @since 2020-12-07
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("eduservice/tearcher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    @GetMapping("/findAll")
    @ApiOperation(value = "所有讲师列表")
    public Result findAll(){
        List<EduTeacher> list = teacherService.list(null);
        return Result.ok().data("tearchlist",list);
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("pageTeacher/{current}/{limit}")
    public Result pageTeacher(
            @ApiParam(name = "current",value = "当前页", required = true)
            @PathVariable Long current,
            @ApiParam(name = "limit",value = "每页展示个数", required = true)
            @PathVariable Long limit){

        Page<EduTeacher> pageTeacher = new Page<>(current,limit);

        IPage<EduTeacher> page = teacherService.page(pageTeacher, null);

        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();


        return Result.ok().data("total",total).data("records",records);

    }

    @ApiOperation(value = "条件分页查询")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public Result pageTeacherCondition(
            @ApiParam(name = "current",value = "当前页", required = true)
            @PathVariable Long current,
            @ApiParam(name = "limit",value = "每页展示个数", required = true)
            @PathVariable Long limit,
            @RequestBody(required = false) TeacherQuery teacherQuery){

//        Page<EduTeacher> pageParam = new Page<>(current, limit);
//
//        teacherService.pageQuery(pageParam,teacherQuery);
//
//        long total = pageParam.getTotal();
//        List<EduTeacher> records = pageParam.getRecords();
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);

        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)) {
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create",end);
        }

        //调用方法实现条件查询分页
        teacherService.page(pageTeacher,wrapper);

        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords(); //数据list集合


        return Result.ok().data("total",total).data("records",records);


    }


    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询讲师")
    public Result getById(
            @ApiParam(name = "id", value = "要查询的讲师id", required = true)
            @PathVariable String id){
        EduTeacher byId = teacherService.getById(id);
        return Result.ok().data("teacher",byId);
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改讲师")
    public Result update(@RequestBody EduTeacher eduTeacher){
        boolean update = teacherService.update(eduTeacher, null);
        if(!update){
            return Result.error();
        }
        return Result.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "逻辑删除讲师")
    public Result delete(@PathVariable String id){
        boolean flag = teacherService.removeById(id);
        if(!flag){
            return Result.error();
        }
        return Result.ok();
    }

    @PostMapping("add")
    @ApiOperation(value = "新增讲师")
    public Result add(@RequestBody EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);
        if(!save){
            return Result.error();
        }
        return Result.ok();
    }
}

